package com.cn.service.Imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.cn.RedisThread.Redis_Set_WorksComment;
import com.cn.entity.Works;
import com.cn.entity.Works_Comment;
import com.cn.mapper.Works.*;
import com.cn.service.IWorkService;
import com.cn.util.Idwoker;
import com.cn.vo.WorksVo.Collect;
import com.cn.vo.WorksVo.Comment;
import com.cn.vo.WorksVo.Like;
import com.cn.vo.WorksVo.Report;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName WorkServiceImp
 * @Description TODO
 * @Author yujiajing
 * @Date 2022/11/18 20:34
 * @Version 1.0
 */
@Service("WorkServiceImp")
public class WorkServiceImp implements IWorkService {
    @Autowired
    Redis_Set_WorksComment redis_set_worksComment;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    WorksMapper worksMapper;
    @Autowired
    LikeMapper likeMapper;
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    CollectMapper collectMapper;
    @Autowired
    ReportMapper reportMapper;
    /**
     *
     * 发布作品
     * @return
     */
    @Override
    public int publishWorks(Works works) {
        works.setWorksId(IdWorker.getId());
        String key="worksId:"+works.getWorksId();
        redisTemplate.opsForValue().set(key,works);
        return  worksMapper.insert(works);

    }

    /**
     * 删除个人发布的作品
     * 原理：依据用户ID和作品ID
     * @param User_id
     * @param Works_id
     * @return
     */
    @Override
    public int deleteWorks(int User_id,int Works_id) {
        QueryWrapper queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("works_id",Works_id);
        queryWrapper.eq("user_id",User_id);
        return  worksMapper.delete(queryWrapper);
    }

    /**
     * 点赞作品
     * @param like
     * @return
     */
    @Override
    public int giveLike(Like like) {
        return likeMapper.insert(like);
    }

    @Override
    public List<Like> showLike() {
        return likeMapper.selectList(null);
    }

    @Override
    public int deleteLike(int workIdsId, String likeName) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("works_Id",workIdsId);
        queryWrapper.eq("like_name",likeName);
       return likeMapper.delete(queryWrapper);
    }

    /**
     * 评论一个作品
     * @param comment
     * @return
     */
    @Override
    public int writeComment(Comment comment) {
        comment.setCommentId(IdWorker.getId());
        String key="comment:"+comment.getCommentId();
        redisTemplate.opsForValue().set(key,comment);
        System.out.println("——————————————————————》加入Redis缓存《——————————————————————");
        return  commentMapper.insert(comment);
    }

    /**
     * 添加一个收藏
     * @param collect
     * @return
     */
    @Override
    public int collectWorks(Collect collect) {
        return collectMapper.insert(collect);
    }

    /**
     * 取消点赞
     * @param likeId
     * @return
     */
    @Override
    public int cancelLike(int worksId,int likeId) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("work_id",worksId);
        queryWrapper.eq("like_id",likeId);
        return likeMapper.delete(queryWrapper);
    }



    @Override
    public List<Works_Comment> showAllComments() {
        return worksMapper.showCommentByWorksId();
    }

    @Override
    public List<Works_Comment> showAllCommentByAdmin(String userName) {
        return worksMapper.showAllcommentByAdmin(userName);
    }

    /**
     * 删除评论
     * @param CommentId
     * @return
     */
    @Override
    public int deleteComment(int WorksId,int CommentId) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("work_id",WorksId);
        queryWrapper.eq("comment_id",CommentId);
        return likeMapper.delete(queryWrapper);
    }

    /**
     * 删除收藏
     * @param WorksId
     * @param CollectorId
     * @return
     */
    @Override
    public int deleteCollect(int WorksId, int CollectorId) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("work_id",WorksId);
        queryWrapper.eq("comment_id",CollectorId);
        return likeMapper.delete(queryWrapper);
    }

    @Override
    public List<Report> showReport() {
        return reportMapper.selectList(null);
    }

    @Override
    public int giveReport(Report report) {
        return reportMapper.insert(report);
    }

    /**
     * 转发评论
     * @param UserId
     * @param WorksId
     * @return
     */
/*    @Override
    public int transmitComment(int UserId, int WorksId) {
        Works works=new Works();
        works.setWorksId(WorksId);
        works.setUserId(UserId);
        return worksMapper.insert(works);
    }*/
}
